package query.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import query.bean.Answer;
import query.bean.Paper;
import query.bean.Question;
import query.db.PaperRepository;
import query.db.QuestionnaireRepository;

/**
 * 答卷控制类
 * 
 * @author lzy
 * @version 1.0.4
 */
@Controller
@RequestMapping("/paper")
public class PaperController {

	/**
	 * 问卷数据库操作对象
	 */
	@Autowired
	private QuestionnaireRepository questionnaireRepository;

	/**
	 * 答卷数据库操作对象
	 */
	@Autowired
	private PaperRepository paperRepository;

	/**
	 * 默认一页展示的问卷数目
	 */
	private final static int PAGE = 4;

	/**
	 * 处理选择题统计结果
	 * 
	 * @param id      答卷id
	 * @param session 用户session
	 * @return 重定向问卷回答情况页面
	 */
	@RequestMapping(value = "/result/{questionnaireId}", method = GET)
	public String getResult(@PathVariable("questionnaireId") long id, HttpSession session) {
		if (id == 0) {
			return "result";
		}
		List<Question> questions = questionnaireRepository.searchQuestions(id);
		List<Paper> papers = paperRepository.searchPaper(id);
		LinkedList<Answer> answers = new LinkedList<Answer>();
		for (int i = 0; i < questions.size(); i++) {
			questions.get(i).setTotal(0);
			questions.get(i).setAnswerA(0);
			questions.get(i).setAnswerB(0);
			questions.get(i).setAnswerC(0);
			questions.get(i).setAnswerD(0);
		}
		if (papers == null) {

		} else {
			for (int i = 0; i < papers.size(); i++) {
				List<Answer> list = paperRepository.searchAnswer(papers.get(i).getId());
				for (int j = 0; j < list.size(); j++) {
					answers.add(list.get(j));
				}
			}
			int total = answers.size() / questions.size();
			int orderNumber;
			for (int i = 0; i < answers.size(); i++) {
				if (answers.get(i).getType() == 1 || answers.get(i).getType() == 2) {
					orderNumber = answers.get(i).getOrderNumber();
					orderNumber--;
					questions.get(orderNumber).setTotal(total);
					if (answers.get(i).getAnswerA().equals("true")) {
						questions.get(orderNumber).setAnswerA(questions.get(orderNumber).getAnswerA() + 1);
					}
					if (answers.get(i).getAnswerB().equals("true")) {
						questions.get(orderNumber).setAnswerB(questions.get(orderNumber).getAnswerB() + 1);
					}
					if (answers.get(i).getAnswerC().equals("true")) {
						questions.get(orderNumber).setAnswerC(questions.get(orderNumber).getAnswerC() + 1);
					}
					if (answers.get(i).getAnswerD().equals("true")) {
						questions.get(orderNumber).setAnswerD(questions.get(orderNumber).getAnswerD() + 1);
					}
				}
			}
		}
		session.setAttribute("questionList", questions);
		return "result";
	}

	/**
	 * 获取问卷详情首页
	 * 
	 * @param id      问卷id
	 * @param session 用户id
	 * @return 问卷详情页面
	 */
	@RequestMapping(value = "/detail/{questionnaireId}", method = GET)
	public String getDetail(@PathVariable("questionnaireId") long id, HttpSession session) {
		session.setAttribute("questionnaireId", id);
		Integer integer = (Integer) session.getAttribute("detailPage");
		if (integer == null) {
			integer = new Integer(1);
			session.setAttribute("detailPage", integer);
		}
		int page = integer.intValue();
		List<Paper> papers = paperRepository.searchPaper(id, (page - 1) * PAGE, PAGE);
		session.setAttribute("paperList", papers);
		return "detail";
	}

	/**
	 * 处理答卷详细信息查看分页
	 * 
	 * @param page    要获取页面的页码
	 * @param session 用户session
	 * @return 操作结果
	 */
	@RequestMapping(value = "/gotoPage", method = POST)
	@ResponseBody
	public String processGotoPage(String page, HttpSession session) {
		int temp = Integer.valueOf(page);
		long id = (long) session.getAttribute("questionnaireId");
		List<Paper> papers = paperRepository.searchPaper(id, (temp - 1) * PAGE, PAGE);
		if (papers.size() != 0) {
			session.setAttribute("detailPage", new Integer(temp));
			session.setAttribute("paperList", papers);
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 返回指定答卷具体信息
	 * 
	 * @param id      答卷id
	 * @param session 用户session
	 * @return 答卷具体信息页面
	 */
	@RequestMapping(value = "/details/{paperId}", method = GET)
	public String processDetails(@PathVariable("paperId") long id, HttpSession session) {
		List<Answer> answers = paperRepository.searchAnswer(id);
		session.setAttribute("answers", answers);
		return "answer";
	}

}
